package com.ghost.view.login
{
	import com.ghost.data_struct.HashMap;
	import com.ghost.event.Pop_win_event;
	import com.ghost.manager.Effect_manager;
	import com.ghost.manager.Load_manager;
	import com.ghost.manager.Source_manager;
	import com.ghost.model.Configure_url;
	import com.ghost.model.Effect_data;
	import com.ghost.model.Global_data;
	import com.ghost.model.Loading_data;
	import com.ghost.model.Object_pool;
	import com.ghost.view.Base_effect;
	import com.ghost.vo.loading.Loading_list;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author ghost
	 * 登录主界面
	 */	
	public class Login_main extends Base_effect
	{
		private var server_mc:Select_server;
		private var role_mc:Select_role;
		private var server_bg_mc:Bitmap;
		private var role_bg_mc:Bitmap;
		public function Login_main()
		{
			Load_manager.get_Instance().add(Configure_url.swf_url+"login_ui.swf",Loading_data.type_swf,Global_data.loading_des_list["login_ui.swf"].des_str,login_ui_Handler);
			Load_manager.get_Instance().add(Configure_url.img_url+'login/server_bg.jpg',Loading_data.type_img,Global_data.loading_des_list['server_bg.jpg'].des_str,server_bg_Handler);
			Load_manager.get_Instance().add(Configure_url.img_url+'login/role_bg.jpg',Loading_data.type_img,Global_data.loading_des_list['role_bg.jpg'].des_str,role_bg_Handler);
			Load_manager.get_Instance().start();
		}
		private function login_ui_Handler(data:Loader):void
		{
			var ui_data_h:HashMap = new HashMap();
			ui_data_h.put('server_tilte',Source_manager.getInstance().getBitmapData('server_tilte'));
			ui_data_h.put('back_out',Source_manager.getInstance().getBitmapData('back_out'));
			ui_data_h.put('back_over',Source_manager.getInstance().getBitmapData('back_over'));
			ui_data_h.put('login_out',Source_manager.getInstance().getBitmapData('login_out'));
			ui_data_h.put('input_bg',Source_manager.getInstance().getBitmapData('input_bg'));
			Object_pool.loaded_obj_pool.put("login_ui",ui_data_h);
		}
		private function role_bg_Handler(data:Loader):void
		{
			role_bg_mc = Bitmap(data.content);
			first_init();
		}
		private function server_bg_Handler(data:Loader):void
		{
			server_bg_mc = Bitmap(data.content);
		}
		public function show_bg(bg:Bitmap=null):void
		{
			if(bg)bg.visible = true;
		}
		/**
		 *初始化登录界面 
		 */		
		public function first_init():void
		{
			if(server_mc == null)
			{
				server_mc = new Select_server(server_bg_mc);
				this.addChild(server_mc);
				server_mc.x = this.width/2-server_mc.width/2;
				server_mc.y = this.height/2-server_mc.height/2;
			}
			server_mc.visible = true;
			Effect_manager.getInstance().set_effect(server_mc,Effect_data.fade_in_obj);
			server_mc.addEventListener("server_end",Server_Handler);
		}
		private function Server_Handler(e:Event):void
		{
			server_mc.removeEventListener("server_end",Server_Handler);
			Effect_manager.getInstance().set_effect(server_mc,Effect_data.fade_out_obj);
			server_mc.addEventListener(Pop_win_event.removed_effect_event,remove_effect);
		}
		private function remove_effect(e:Pop_win_event):void
		{
			server_mc.removeEventListener(Pop_win_event.removed_effect_event,remove_effect);
			server_mc.visible = false;
			show_role();
		}
		public function show_role():void
		{
			if(role_mc == null)
			{
				role_mc = new Select_role(role_bg_mc);
				this.addChild(role_mc);
				role_mc.x = this.width/2-role_mc.width/2;
				role_mc.y = this.height/2-role_mc.height/2;
			}
			role_mc.visible = true;
			Effect_manager.getInstance().set_effect(role_mc,Effect_data.fade_in_obj);
			role_mc.addEventListener('back',back_Handler);//返回到服务器选择
			role_mc.addEventListener("role_end",role_Handler);
		}
		private function role_Handler(e:Event):void
		{
			this.parent.dispatchEvent(new Event('into_main'));
		}
		private function back_Handler(e:Event):void
		{
			trace('back');
			Effect_manager.getInstance().set_effect(role_mc,Effect_data.fade_out_obj);
			role_mc.addEventListener(Pop_win_event.removed_effect_event,remove_role_effect);
		}
		private function remove_role_effect(e:Pop_win_event):void
		{
			role_mc.removeEventListener(Pop_win_event.removed_effect_event,remove_role_effect);
			role_mc.visible = false;
			first_init();
		}
	}
}